﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace Utilities
{
    /// <summary>
    /// 字符串操作工具集
    /// </summary>
    public class StringUtil
    {


        #region SubRealLength
        /// <summary>
        /// sub_realLength
        /// </summary>
        /// <param name="source"></param>
        /// <param name="StarIndex"></param>
        /// <param name="Length"></param>
        /// <returns></returns>
        public static string SubRealLength(string source, int StarIndex, int Length)
        {
            int count = 0;
            string tempStr = "";
            char[] tempChar = source.ToCharArray();
            for (int i = 0; i < tempChar.Length; i++)
            {
                if (IsDoubleByte(tempChar[i]))
                {
                    count += 2;
                }
                else
                {
                    count += 1;
                }
                if ((count >= StarIndex) && (realLenth(tempStr) < Length))
                {
                    tempStr += tempChar[i];
                }
            }
            return tempStr;
        }

        #endregion



        #region 字符串截取处理

        /// <summary>
        /// 字符串截取处理
        /// </summary>
        /// <param name="SourceString">要处理的字符串</param>
        /// <param name="StarString">起始字符</param>
        /// <param name="EndString">终止字符</param>
        /// <returns>截取后的字符或空字符</returns>
        public static string SubString(string SourceString, string StarString, string EndString)
        {
            int StarIndex = SourceString.IndexOf(StarString) + StarString.Length;
            int endIndex = SourceString.IndexOf(EndString);
            if (StarIndex != -1 && endIndex != -1)
            {
                return SourceString.Substring(StarIndex, endIndex - StarIndex);
            }
            else
            {
                return "";
            }
        }
        #endregion

        #region print_page
        public static string print_page(string sourceString, int lineLength, int pageLineCount, string breakText, bool IsBreak)
        {
            string[] FirstAsunder = newLineToArray(sourceString);
            return Left(pageBreak(FirstAsunder, breakText, pageLineCount, lineLength, IsBreak), 1);


        }
        #endregion

        #region pageBreak
        public static string pageBreak(string[] sourceString, string breakText, int pageLineCount, int lineLength, bool IsBreak)
        {
            string tempStr = "";
            for (int i = 0; i < sourceString.Length; i++)
            {
                if (IsBreak)
                {
                    tempStr += lineBreak(sourceString[i], lineLength) + "\n";
                }
                else
                {
                    tempStr += sourceString[i] + "\n";
                }
            }
            tempStr = Left(tempStr, 1);
            string[] tempArray = tempStr.Split('\n');
            tempStr = "";
            for (int i = 0; i < tempArray.Length; i++)
            {
                //tempStr+=i.ToString();
                tempStr += tempArray[i];
                if ((i + 1) % pageLineCount == 0)
                {
                    tempStr += breakText;
                }
                else
                {
                    tempStr += "\n";
                }
            }
            return tempStr;
        }
        #endregion

        #region Left
        /// <summary>
        /// Left
        /// </summary>
        /// <param name="sourceString"></param>
        /// <param name="i"></param>
        /// <returns></returns>
        public static string Left(string sourceString, int i)
        {
            if (sourceString.Length > 0)
            {
                return sourceString.Substring(0, sourceString.Length - i);
            }
            else
            {
                return "";
            }
        }
        #endregion

        #region isDoubleByte
        /// <summary>
        /// isDoubleByte
        /// </summary>
        /// <param name="sourceChar"></param>
        /// <returns></returns>
        public static bool IsDoubleByte(char sourceChar)
        {
            int i = realLenth(sourceChar.ToString());
            if (i == 1)
            {
                return false;
            }
            else
            {
                return true;
            }
        }

        #endregion

        #region lineBreak
        public static string lineBreak(string sourceString, int lineLength)
        {
            charConvert(sourceString);
            char[] tempChar = sourceString.ToCharArray();
            int Count = 0;
            string tempStr = "";
            for (int i = 0; i < tempChar.Length; i++)
            {
                if (IsDoubleByte(tempChar[i]) == true)
                {
                    Count += 2;
                }
                else
                {
                    Count += 1;
                }
                tempStr += tempChar[i];
                if ((Count % lineLength) == (lineLength - 1))
                {
                    if (i < (tempChar.Length - 1))
                    {
                        if (IsDoubleByte(tempChar[i + 1]))
                        {
                            tempStr += "\n";
                        }
                    }
                }
                if ((Count % lineLength) == 0 && (Count != 0))
                {
                    tempStr += "\n";
                }
            }
            return tempStr;
        }
        #endregion

        #region charConvert
        public static string charConvert(string sourceString)
        {
            return sourceString.Replace("\t", "        ");
        }
        public static string[] newLineToArray(string sourceString)
        {
            return sourceString.Split('\n');
        }
        #endregion

        #region 获取字符串的实际长度
        ///<summary>
        ///获取字符串的实际长度
        ///</summary>
        ///<param name="sourceString">源字符串</param>
        public static int realLenth(string sourceString)
        {
            Byte[] tLength = System.Text.Encoding.Default.GetBytes(sourceString);
            return tLength.Length;
        }
        #endregion

        #region 从字符串中的尾部删除指定的字符串
        /// <summary>
        /// 从字符串中的尾部删除指定的字符串
        /// </summary>
        /// <param name="sourceString"></param>
        /// <param name="removedString"></param>
        /// <returns></returns>
        public static string Remove(string sourceString, string removedString)
        {
            try
            {
                if (sourceString.IndexOf(removedString) < 0)
                    throw new Exception("原字符串中不包含移除字符串！");
                string result = sourceString;
                int lengthOfSourceString = sourceString.Length;
                int lengthOfRemovedString = removedString.Length;
                int StarIndex = lengthOfSourceString - lengthOfRemovedString;
                string tempSubString = sourceString.Substring(StarIndex);
                if (tempSubString.ToUpper() == removedString.ToUpper())
                {
                    result = sourceString.Remove(StarIndex, lengthOfRemovedString);
                }
                return result;
            }
            catch
            {
                return sourceString;
            }
        }
        #endregion

        #region 获取拆分符右边的字符串
        /// <summary>
        /// 获取拆分符右边的字符串
        /// </summary>
        /// <param name="sourceString"></param>
        /// <param name="splitChar"></param>
        /// <returns></returns>
        public static string RightSplit(string sourceString, char splitChar)
        {
            string result = null;
            string[] tempString = sourceString.Split(splitChar);
            if (tempString.Length > 0)
            {
                result = tempString[tempString.Length - 1].ToString();
            }
            return result;
        }
        #endregion

        #region 获取拆分符左边的字符串
        /// <summary>
        /// 获取拆分符左边的字符串
        /// </summary>
        /// <param name="sourceString"></param>
        /// <param name="splitChar"></param>
        /// <returns></returns>
        public static string LeftSplit(string sourceString, char splitChar)
        {
            string result = null;
            string[] tempString = sourceString.Split(splitChar);
            if (tempString.Length > 0)
            {
                result = tempString[0].ToString();
            }
            return result;
        }
        #endregion

        #region  去掉最后一个逗号
        /// <summary>
        /// 去掉最后一个逗号
        /// </summary>
        /// <param name="origin"></param>
        /// <returns></returns>
        public static string DelLastComma(string origin)
        {
            if (origin.IndexOf(",") == -1)
            {
                return origin;
            }
            return origin.Substring(0, origin.LastIndexOf(","));
        }
        #endregion

        #region 删除不可见字符
        /// <summary>
        /// 删除不可见字符
        /// </summary>
        /// <param name="sourceString"></param>
        /// <returns></returns>
        public static string DeleteUnVisibleChar(string sourceString)
        {
            System.Text.StringBuilder sBuilder = new System.Text.StringBuilder(131);
            for (int i = 0; i < sourceString.Length; i++)
            {
                int Unicode = sourceString[i];
                if (Unicode >= 16)
                {
                    sBuilder.Append(sourceString[i].ToString());
                }
            }
            return sBuilder.ToString();
        }
        #endregion

        #region 获取数组元素的合并字符串
        /// <summary>
        /// 获取数组元素的合并字符串
        /// </summary>
        /// <param name="stringArray"></param>
        /// <returns></returns>
        public static string GetArrayString(string[] stringArray)
        {
            string totalString = null;
            for (int i = 0; i < stringArray.Length; i++)
            {
                totalString = totalString + stringArray[i];
            }
            return totalString;
        }
        #endregion

        #region 获取某一字符串在字符串数组中出现的次数
        /// <summary>
        ///		获取某一字符串在字符串数组中出现的次数
        /// </summary>
        /// <param name="stringArray" type="string[]">
        ///     <para>
        ///         
        ///     </para>
        /// </param>
        /// <param name="findString" type="string">
        ///     <para>
        ///         
        ///     </para>
        /// </param>
        /// <returns>
        ///     A int value...
        /// </returns>
        public static int GetStringCount(string[] stringArray, string findString)
        {
            int count = -1;
            string totalString = GetArrayString(stringArray);
            string subString = totalString;

            while (subString.IndexOf(findString) >= 0)
            {
                subString = totalString.Substring(subString.IndexOf(findString));
                count += 1;
            }
            return count;
        }
        #endregion

        #region 获取某一字符串在字符串中出现的次数
        /// <summary>
        ///     获取某一字符串在字符串中出现的次数
        /// </summary>
        /// <param name="stringArray" type="string">
        ///     <para>
        ///         原字符串
        ///     </para>
        /// </param>
        /// <param name="findString" type="string">
        ///     <para>
        ///         匹配字符串
        ///     </para>
        /// </param>
        /// <returns>
        ///     匹配字符串数量
        /// </returns>
        public static int GetStringCount(string sourceString, string findString)
        {
            int count = 0;
            int findStringLength = findString.Length;
            string subString = sourceString;

            while (subString.IndexOf(findString) >= 0)
            {
                subString = subString.Substring(subString.IndexOf(findString) + findStringLength);
                count += 1;
            }
            return count;
        }
        #endregion

        #region 获取某一字符串在字符串中出现的次数
        public static int GetStringCount1(string sourceString, string findString)
        {
            int count = 0;
            if (sourceString.Trim().Equals(""))
            {
                count = 0;
            }
            else
            {
                count = sourceString.Replace(findString, findString + "*").Length - sourceString.Length;
            }

            return count;
        }
        #endregion

        #region 截取从StarString开始到原字符串结尾的所有字符
        /// <summary>
        /// 截取从StarString开始到原字符串结尾的所有字符   
        /// </summary>
        /// <param name="sourceString" type="string">
        ///     <para>
        ///         
        ///     </para>
        /// </param>
        /// <param name="StarString" type="string">
        ///     <para>
        ///         
        ///     </para>
        /// </param>
        /// <returns>
        ///     A string value...
        /// </returns>
        public static string GetSubString(string sourceString, string StarString)
        {
            try
            {
                int index = sourceString.ToUpper().IndexOf(StarString);
                if (index > 0)
                {
                    return sourceString.Substring(index);
                }
                return sourceString;
            }
            catch
            {
                return "";
            }
        }

        public static string GetSubString(string sourceString, string beginRemovedString, string endRemovedString)
        {
            try
            {
                if (sourceString.IndexOf(beginRemovedString) != 0)
                    beginRemovedString = "";

                if (sourceString.LastIndexOf(endRemovedString, sourceString.Length - endRemovedString.Length) < 0)
                    endRemovedString = "";

                int StarIndex = beginRemovedString.Length;
                int length = sourceString.Length - beginRemovedString.Length - endRemovedString.Length;
                if (length > 0)
                {
                    return sourceString.Substring(StarIndex, length);
                }
                return sourceString;
            }
            catch
            {
                return sourceString; ;
            }
        }
        #endregion


        #region 按字节截取字符串 2007.9.6
        /// <summary>
        /// 按字节截取字符串
        /// </summary>
        /// <param name="s">输入</param>
        /// <param name="len">长度</param>
        /// <param name="symbol">结束的符号</param>
        /// <returns></returns>
        public static string CutStringByte(string s, int len, string symbol)
        {
            string result = ""; //最终返回的结果
            int byteLen = System.Text.Encoding.Default.GetByteCount(s);  //单字节字符长度
            int charLen = s.Length; //把字符平等对待时的字符串长度
            int byteCount = 0;  //记录读取进度{中文按两单位计算}
            int pos = 0;    //记录截取位置{中文按两单位计算}
            if (byteLen > len)
            {
                for (int i = 0; i < charLen; i++)
                {
                    if (Convert.ToInt32(s[i]) > 255)  //遇中文字符计数加2
                        byteCount += 2;
                    else         //按英文字符计算加1
                        byteCount += 1;

                    if (byteCount >= len)   //到达指定长度时，记录指针位置并停止
                    {
                        pos = i + 1;//表明第几个字符
                        break;
                    }
                }
                result = s.Substring(0, pos);
            }
            else
                result = s;

            if (result.Length < s.Length) //是否加符号 
                result += symbol;
            return result;
        }

        #endregion

        #region 按字节数取出字符串的长度
        /// <summary>
        /// 按字节数取出字符串的长度
        /// </summary>
        /// <param name="strTmp">要计算的字符串</param>
        /// <returns>字符串的字节数</returns>
        public static int GetByteCount(string strTmp)
        {
            int intCharCount = 0;
            for (int i = 0; i < strTmp.Length; i++)
            {
                if (System.Text.UTF8Encoding.UTF8.GetByteCount(strTmp.Substring(i, 1)) == 3)
                {
                    intCharCount = intCharCount + 2;
                }
                else
                {
                    intCharCount = intCharCount + 1;
                }
            }
            return intCharCount;
        }
        #endregion

        #region 按字节数要在字符串的位置
        /// <summary>
        /// 按字节数要在字符串的位置
        /// </summary>
        /// <param name="intIns">字符串的位置</param>
        /// <param name="strTmp">要计算的字符串</param>
        /// <returns>字节的位置</returns>
        public static int GetByteIndex(int intIns, string strTmp)
        {
            int intReIns = 0;
            if (strTmp.Trim() == "")
            {
                return intIns;
            }
            for (int i = 0; i < strTmp.Length; i++)
            {
                if (System.Text.UTF8Encoding.UTF8.GetByteCount(strTmp.Substring(i, 1)) == 3)
                {
                    intReIns = intReIns + 2;
                }
                else
                {
                    intReIns = intReIns + 1;
                }
                if (intReIns >= intIns)
                {
                    intReIns = i + 1;
                    break;
                }
            }
            return intReIns;
        }
        #endregion

        #region 返回添加头尾特殊字符字符串
        /// <summary>
        /// 返回添加头尾特殊字符字符串，并替换字符串内特殊字符串
        /// </summary>
        /// <param name="extStr">特殊字符 单个字符不为空 例如 "|" </param>
        /// <param name="tmpStr">形如 "","|0|1|2|","0|1|2","0|1|2|","|0|1|2","0|1|2","0"</param>
        /// <returns>		返回	"","|0|1|2|","|0|1|2|","|0|1|2|","|0|1|2|","|0|1|2|","|0|"</returns>
        public static string addHeadTailStr(string tmpStr, string extStr)
        {
            tmpStr = tmpStr.Replace(" ", "");	//去除空格
            if (tmpStr.Equals(""))
                return "";


            if (!tmpStr.Substring(0, 1).Equals(extStr))
                tmpStr = extStr + tmpStr;
            if (!tmpStr.Substring(tmpStr.Length - 1, 1).Equals(extStr))
                tmpStr += extStr;
            return tmpStr;
        }
        #endregion

        #region 返回特殊字符字符串最后数据
        /// <summary>
        /// 返回特殊字符字符串最后数据
        /// </summary>
        /// <param name="extStr">特殊字符 单个字符不为空 例如 "|" </param>
        /// <param name="tmpStr">形如 "","|0|1|2|","0|1|","|0|"</param>
        /// <returns>		返回	"","2","1","0"</returns>
        public static string CutStrStadium(string tmpStr)
        {
            string[] strArraytem;
            tmpStr = tmpStr.Replace(" ", "");	//去除空格
            if (tmpStr.Equals(""))
                return "";

            strArraytem = tmpStr.Split(new char[1] { '|' });
            tmpStr = strArraytem[strArraytem.Length - 2];

            return tmpStr;
        }
        #endregion

        #region 返回去头尾特殊字符字符串
        /// <summary>
        /// 返回去头尾特殊字符字符串，并替换字符串内特殊字符串
        /// </summary>
        /// <param name="extStr">特殊字符 单个字符不为空 例如 "|" </param>
        /// <param name="tmpStr">形如 "","||","|0|1|2|","0|1|2","0|1|2|","|0|1|2","0|1|2","0"</param>
        /// <returns>		返回 "","","0|1|2","0|1|2","0|1|2","0|1|2","0|1|2","0"</returns>
        public static string delHeadTailStr(string tmpStr, string extStr)
        {
            tmpStr = tmpStr.Replace(" ", "");	//去除空格
            if (tmpStr.Equals(""))
                return "";

            // 没有直接返回原值
            int idx = tmpStr.IndexOf(extStr);
            if (idx == -1)
                return tmpStr;

            // 检查头字符
            while (idx == 0)
            {
                if (tmpStr.Equals(""))
                    return "";
                else
                    tmpStr = tmpStr.Substring(1, tmpStr.Length - 1);
                idx = tmpStr.IndexOf(extStr);
            }

            // 检查尾字符串
            idx = tmpStr.LastIndexOf(extStr);
            if (idx == -1)
                return tmpStr;
            while (idx == (tmpStr.Length - 1))
            {
                if (tmpStr.Equals(""))
                    return "";
                else
                    tmpStr = tmpStr.Substring(0, tmpStr.Length - 1);
                idx = tmpStr.LastIndexOf(extStr);
            }
            return tmpStr;
        }
        #endregion

        #region 返回加引号字符串 常用于sql 中的in 关键字
        /// <summary>
        /// 返回去头尾特殊字符字符串，并替换字符串内特殊字符串
        /// </summary>
        /// <param name="oStr">形如必须以逗号间隔,头尾无逗号 形如："","0","1,2"</param>
        /// <returns>		   返回								   "","'0'","'1','2'"</returns>
        public static string getSqlForInStr(string tmpStr)
        {
            tmpStr = tmpStr.Replace(" ", "");	//替换空格
            if (tmpStr.Equals(""))
                return "";
            tmpStr = tmpStr.Replace(",", "','");	//把所有逗号替换为 ','
            return tmpStr = "'" + tmpStr + "'";
        }
        #endregion

        // Star new.simon20061104
        #region 将带 "|" 的字符串转化为sql语句in关键字可用字符串
        /// <summary>
        /// 将带 "|" 的字符串转化为sql语句in关键字可用字符串
        /// </summary>
        /// <param name="strTmp">输入字符串 例如 "|2|3|4|5|6|7|"</param>
        /// <returns>得到 "'2','3','4','5','6','7'"</returns>
        public static string getInStr(string strTmp)
        {
            // 处理头尾问题
            strTmp = delHeadTailStr(strTmp, "|");
            // 将"|"替换为","
            strTmp = strTmp.Replace("|", ",");
            // 将所有","换为"','"
            strTmp = getSqlForInStr(strTmp);
            return strTmp;
        }
        #endregion
        // end new.simon20061104

        #region 将带 "," 的字符串转化为sql语句in关键字可用字符串 'a','b'
        /// <summary>
        /// 将带 "," 的字符串转化为sql语句in关键字可用字符串 'a','b'
        /// </summary>
        /// <param name="strTmp">输入字符串 例如 ",2,3,4"</param>
        /// <returns>得到 "'2','3','4'"</returns>
        public static string getInStr1(string strTmp)
        {
            // 处理头尾问题
            strTmp = delHeadTailStr(strTmp, ",");
            // 将所有","换为"','"
            strTmp = getSqlForInStr(strTmp);
            return strTmp;
        }
        #endregion


        #region 获得由特殊字符间隔的字符串的第一个有效字符串值
        /// <summary>
        /// 获得由特殊字符间隔的字符串的第一个有效字符串值
        /// </summary>
        /// <param name="tmpStr">,,1,2,3,</param>
        /// <param name="extChar">,</param>
        /// <returns>返回1</returns>
        public static string GetFirstValidStr(string tmpStr, char extChar)
        {
            tmpStr = tmpStr.Trim();
            if (tmpStr.Equals(""))
                return tmpStr;

            string turnStr = "";
            string[] aryStr = tmpStr.Split(new char[] { extChar });
            for (int i = 0; i < aryStr.Length; i++)
            {
                if (aryStr[i] != null && !aryStr[i].Trim().Equals(""))
                {
                    turnStr = aryStr[i].Trim();
                    break;
                }
            }
            return turnStr;
        }
        #endregion

        //begin NEW.JZ20061102
        #region 在字符串前面补零
        public static string Add0B4Str(string str, int length)
        {
            string result = str;
            if (str.Length < length)
            {
                for (int i = 0; i < length - str.Length; i++)
                {
                    result = "0" + result;
                }
            }

            return result;
        }
        #endregion



        #region 用指定字符替换字符串中指定位的字符
        /// <summary>
        /// 用指定字符替换字符串中指定位的字符 如 ReplaceStr('abdeg',2,'c') 返回 abceg
        /// </summary>
        /// <param name="str">目标字符串</param>
        /// <param name="replaceindex">要替换的字符串的索引</param>
        /// <param name="replacestr">替换的字符串</param>
        /// <returns></returns>
        public static string ReplaceStr(string str, int replaceindex, string replacestr)
        {
            if (replaceindex < 0)
                return "";
            else if (str.Trim().Equals(""))
                return str;
            else if (str.Length < replaceindex + 1)
                return "";
            else
            {
                str = str.Insert(replaceindex, replacestr);
                str = str.Remove(replaceindex + 1, 1);
                return str;
            }
        }
        #endregion

        #region 返回字符串中指定位子的字符
        public static string getStrByIndex(string str, int index, int length)
        {
            if (index > str.Length - 1)
                return "";
            else
                return str.Substring(index, length);
        }
        #endregion

        #region 格式化内容，如回车、空格等的显示
        public static string showSpaceEnter(string str)
        {
            str = str.Replace("\r\n", "\r\n<br>");
            str = str.Replace("\t", "&nbsp;&nbsp;");
            str = str.Replace(" ", "&nbsp;");
            return str;
        }

        public static string showReplaceHTML(string str)
        {
            str = str.Replace("<", "&lt;");
            str = str.Replace(">", "&gt;");
            str = str.Replace("\r\n", "<br>");
            str = str.Replace("\r", "<br>");
            str = str.Replace("\n", "<br>");
            str = str.Replace("\t", "&nbsp;&nbsp;");
            str = str.Replace(" ", "&nbsp;");
            return str;
        }
        #endregion
        // end  New.JZ20061102

        #region 字符串截取函数
        /// <summary>
        /// 
        /// </summary>
        /// <param name="inputString">字符串</param>
        /// <param name="len">长度</param>
        /// <returns></returns>
        public static string CutString(string inputString, int len, string strAdd)
        {
            string StrCut = "";
            len = len * 2;
            byte[] bwrite = Encoding.GetEncoding("GB2312").GetBytes(inputString.ToCharArray());
            if (bwrite.Length > len)
            {
                StrCut = Encoding.Default.GetString(bwrite, 0, len).Replace("\0", "");
                return StrCut + strAdd;
            }
            else
                return Encoding.Default.GetString(bwrite);
        }
        #endregion

        // Star new.simon20070326
        #region 判断字符串中的某一位是否为1
        /// <summary>
        /// 判断字符串中的某一位是否为1
        /// </summary>
        /// <param name="sourceStr">原字符串</param>
        /// <param name="flagPos">判断的字符位置</param>
        /// <returns>返回是否为1的结果</returns>
        public static bool isTrueFlag(string sourceStr, int flagPos)
        {
            if (sourceStr == null || sourceStr.Trim().Equals(""))
                return false;
            if (sourceStr.Length < flagPos + 1)
                return false;
            if (sourceStr.Substring(flagPos, 1).Equals("1"))
                return true;
            else
                return false;
        }
        #endregion
        // end new.simon20070326


        /// <summary>
        /// base64编码
        /// </summary>
        /// <param name="strString">将要编码的字符串</param>
        /// <returns>编码后的字符串</returns>
        public static string EnBase64(string strString)
        {
            if (strString.Length != 0)
            {
                //System.Text.Encoding ens = System.Text.Encoding.GetEncoding(54936);
                return Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(strString));
            }
            return strString;
        }

        /// <summary>
        /// base64解码
        /// </summary>
        /// <param name="strString">简要解码的字符串</param>
        /// <returns>解码后的字符串</returns>
        public static string DeBase64(string strString)
        {
            if (strString.Length != 0)
            {
                //System.Text.Encoding ens = System.Text.Encoding.GetEncoding(54936);
                return System.Text.Encoding.UTF8.GetString(Convert.FromBase64String(strString));
            }
            return strString;
        }

        /// <summary>
        /// 格式化字符串
        /// </summary>
        /// <param name="strString">将要格式化的字符串</param>
        /// <returns>格式化后的字符串</returns>
        public static string KeepFormat(string strString)
        {
            string str;
            str = strString;
            if (str != null)
            {
                str = str.Replace(" ", "");
                str = str.Replace("<br>", "");
                str = str.Replace(string.IsInterned("9").ToString(), "");
                str = str.Replace(string.IsInterned("34").ToString(), "");
                str = str.Replace(string.IsInterned("39").ToString(), "");
                str = str.Replace(string.IsInterned("13").ToString() + string.IsInterned("10").ToString(), "");
            }
            return str;
        }
        /// <summary>
        /// 过滤字符串
        /// </summary>
        /// <param name="strString">将要过滤的字符串</param>
        /// <returns>过滤后的字符串</returns>
        public static string Keep(string strString)
        {
            string str;
            str = strString;
            if (str != null)
            {
                str = str.Replace("'", "''");
                str = str.Replace(" ", "&nbsp;");
                str = str.Replace("<", "&lt");
                str = str.Replace(">", "&gt");
                str = str.Replace("|", "");

                str = str.Replace(string.IsInterned("9").ToString(), "&nbsp;");
                str = str.Replace(string.IsInterned("34").ToString(), "&quot;");
                str = str.Replace(string.IsInterned("39").ToString(), "&#39;");
                str = str.Replace(string.IsInterned("13").ToString() + string.IsInterned("10").ToString(), "<br>");
                str = str.Replace(string.IsInterned("10").ToString(), "");
            }
            return str;
        }

        /// <summary>
        /// 过滤js字符串
        /// </summary>
        /// <param name="strString">将要过滤js的字符串</param>
        /// <returns>过滤js后的字符串</returns>
        public static string Fixjs(string strString)
        {
            string str;
            str = strString;
            if (str != null)
            {
                str = str.Replace("\\", "\\\\");
                str = str.Replace(((char)34).ToString(), "\\\"");
                str = str.Replace(((char)39).ToString(), "\\'");
                str = str.Replace(((char)13).ToString(), "\\n");
                str = str.Replace(((char)10).ToString(), "\\r");
                str = str.Replace("'", "&#39;");
            }
            return str;
        }

        public static string ToHtml(string str)
        {
            str = str.Replace(">", "&gt;");
            str = str.Replace("<", "&lt;");
            str = str.Replace(" ", "&nbsp;");
            str = str.Replace("\"", "&quot;");
            //str = str.Replace("", "&nbsp;");
            //str = str.Replace("", "&quot;");
            //str = str.Replace("", "&#39;");
            str = str.Replace("\n", "<br>");
            str = str.Replace("\n\r", "<br>");
            return str;
        }

        /// <summary>
        /// 转换js字符串
        /// </summary>
        /// <param name="strString">将要转换js的字符串</param>
        /// <returns>转换js后的字符串</returns>
        public static string EnFixjs(string strString)
        {
            string str;
            str = strString;
            if (str != null)
            {
                str = str.Replace("\\\\", "\\");
                str = str.Replace("\\\"", ((char)34).ToString());
                str = str.Replace("\\'", ((char)39).ToString());
                str = str.Replace("\\n", ((char)13).ToString());
                str = str.Replace("\\r", ((char)10).ToString());
                str = str.Replace("&#39;", "'");
            }
            return str;
        }



        /// <summary> 
        /// 字符串编码 
        /// </summary> 
        /// <param name="inputData"></param> 
        /// <returns></returns> 
        public static string HtmlEncode(string inputData)
        {
            return System.Web.HttpUtility.HtmlEncode(inputData);
        }

        /// <summary>
        /// 过滤字符
        /// </summary>
        /// <param name="strString"></param>
        /// <returns></returns>
        public string FiltRemoteForbidStr(string strString)
        {
            string tempstr = "";
            tempstr = strString;
            if (tempstr != null && tempstr != "")
            {
                tempstr = tempstr.Replace("'", "''");
                //tempstr = tempstr.Replace("|","");
                //tempstr = tempstr.Replace("[","");
                //tempstr = tempstr.Replace("(","");
                //tempstr = tempstr.Replace(";","");
                //tempstr = tempstr.Replace("%","");
                //tempstr = tempstr.Replace("-","");
                //tempstr = tempstr.Replace("=","");
                //tempstr = tempstr.Replace(".","");
            }
            return tempstr;
        }

        /// <summary> 
        /// 检查字符串最大长度，返回指定长度的串 
        /// </summary> 
        /// <param name="sqlInput">输入字符串</param> 
        /// <param name="maxLength">最大长度</param> 
        /// <returns></returns>			 
        public static string SqlText(string sqlInput, int maxLength)
        {
            if (sqlInput != null && sqlInput != string.Empty)
            {
                sqlInput = sqlInput.Trim();
                if (sqlInput.Length > maxLength)//按最大长度截取字符串 
                    sqlInput = sqlInput.Substring(0, maxLength);
            }
            return sqlInput;
        }
        /// <summary>
        /// 取汉字的长度
        /// </summary>
        /// <param name="strString">原字符串</param>
        /// <param name="strLen">取汉字个数</param>
        /// <returns>截取后的字符串</returns>
        public string LeftTrue(string strString, int strLenth)
        {
            if (strString != null)
            {
                int i, c, t;
                char[] charArr = strString.ToCharArray();

                if (charArr.Length <= strLenth)
                {
                    return strString;
                }
                else
                {
                    t = 0;
                    for (i = 0; i < charArr.Length; i++)
                    {
                        c = charArr[i];
                        if (c < 0)
                            c += 65536;
                        if (c > 255)
                            t += 2;
                        else
                            t++;
                        if (t > strLenth * 2)
                            break;
                    }
                    return new string(charArr, 0, i);
                }
            }
            else
            {
                return strString;
            }
        }

        #region 过滤sql 中的 字符
        /// <summary>
        /// 过滤sql中的 字符
        /// </summary>
        /// <param name="parmText"></param>
        /// <returns></returns>
        public static string SqlFilter(string parmText)
        {
            return parmText.Replace("'", "‘");
        }

        public static string SqlRegress(string parmText)
        {
            return parmText.Replace("‘", "'");
        }

        #endregion


        #region 将浮点型字符串输出为整型字符串
        /// <summary>
        /// 将浮点型字符串输出为整型字符串
        /// </summary>
        /// <param name="floatStr"></param>
        /// <returns></returns>
        public static string floatToIntStr(string floatStr)
        {
            string intStr = floatStr;
            if (floatStr.IndexOf(".") > 0)
                intStr = floatStr.Substring(0, floatStr.IndexOf("."));
            return intStr;
        }
        #endregion

        #region 将浮点型字符串输出去无用零无用点字符串
        /// <summary>
        /// 将浮点型字符串输出去无用零无用点字符串
        /// </summary>
        /// <param name="floatStr">输入 30.10</param>
        /// <returns></returns>
        public static string trimZeroPoint(string floatStr)
        {
            if (floatStr == null || floatStr.Trim().Equals(""))
                return "";

            floatStr = floatStr.IndexOf(".") >= 0 ? floatStr.Trim().TrimEnd('0').TrimEnd('.') : floatStr;

            return floatStr;
        }
        #endregion



        //表示形式 %数字%
        public static string Format(string input, params object[] parm)
        {

            return Regex.Replace(input, @"%(?<num>\d+)%", delegate(Match match)
            {
                int index = int.Parse(match.Groups["num"].Value);
                return parm[index].ToString();
            });
        }




        /// <summary>
        /// 连接字符串
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="separator">分隔符号</param>
        /// <param name="list">参数</param>
        /// <param name="isRemoveEmptyOrNull">是否移除Empty或者Null</param>
        /// <returns>连接后的字符串</returns>
        public static string Join<T>(string separator, IEnumerable<T> list, bool isRemoveEmptyOrNull)
        {
            return Join(separator, "", "", list, isRemoveEmptyOrNull);
        }



        /// <summary>
        /// 连接字符串
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="separator">分隔符号</param>
        /// <param name="list">列表</param>
        /// <returns>连接后的字符串</returns>
        public static string Join<T>(string separator, IEnumerable<T> list)
        {
            return Join(separator, list, false);
        }

        public static string Join<T>(string separator, string leftSymbol, string rightSymbol, IEnumerable<T> list, bool isRemoveEmptyOrNull)
        {
            return Join(separator, leftSymbol, rightSymbol, list, isRemoveEmptyOrNull, false);
        }





        /// <summary>
        /// 连接字符串
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="separator">分隔符号</param>
        /// <param name="leftSymbol">左边的符号</param>
        /// <param name="rightSymbol">右边的符号</param>
        /// <param name="list">数据列表</param>
        /// <param name="isRemoveEmptyOrNull">是否移除空</param>
        /// <returns>连接后的字符串</returns>
        public static string Join<T>(string separator, string leftSymbol, string rightSymbol, IEnumerable<T> list, bool isRemoveEmptyOrNull, bool isSqlFilter)
        {
            StringBuilder sb = new StringBuilder();
            foreach (T item in list)
            {
                if (isRemoveEmptyOrNull && (item == null || item.ToString().Length == 0))
                    continue;
                if (sb.Length > 0)
                    sb.Append(separator);
                if (item != null)
                {
                    sb.Append(leftSymbol);
                    if (isSqlFilter)
                        sb.Append(item.ToString().SqlFilter());
                    else
                        sb.Append(item.ToString());
                    sb.Append(rightSymbol);
                }
            }
            return sb.ToString();
        }

    }
}
